<?php

// 动态获取库存

if (!empty($_GET['stock'])) {
    $stock = $_GET['stock'];
    
    echo getInventory($stock);
}

function getInventory ($stock)
{
    include_once WEBROOT . '/module/inventory/config.inc.php';
    $inventory_config = new config();
    global $db;
    
    $product = explode(',', $stock);
    
    foreach ($product as $item) {
        
        $_t = explode('_', $item);
        
        $ids[] = $_t[0];
        $nums[$_t[0]] = $_t[1];
    }
    
    $scl = 'and id in (' . implode(',', $ids) . ')';
    $scl2='and productid in ('.implode(',', $ids).')';
    
    $sql = "SELECT id, stocks,canbuyout FROM " . $inventory_config->db_prefix .
             "products where 1 $scl order by id asc";
    // ====================
    $db->query($sql);
    $returns = $db->getRows();
    
    //锁定的库存
    $sql="SELECT sum(number) as locked,productid FROM ".$inventory_config->db_prefix."orders where timestocked=0 $scl2 group by productid";
    $db->query($sql);
    $lock=$db->getRows();
    
    foreach ($lock as $item){
        $locked[$item['productid']]=$item['locked'];
    }
    //==================================
    
    $result_stock = 0;
    foreach ($returns as $item) {
    	if ($nums[$item['id']]){
	    $item_stock=$item['stocks']-$item['canbuyout']-$locked[$item['id']];
	    $_t=floor($item_stock/$nums[$item['id']]);
	    
	    }else{
	        $_t=0;
	    }        if ($result_stock == 0) {
            $result_stock = $_t;
        } elseif ($result_stock > $_t) {
            $result_stock = $_t;
        }
    }
    
    
    return $result_stock;
}

//动态减库存
function reduceInventory($stock_relations,$num,$oid,$stock_shop){
		global $config;
    include_once $config['webroot'] . '/module/inventory/config.inc.php';
    $inventory_config = new config();
    global $db;
    $product = explode(',', $stock_relations);
    $db->query("select id,title from {$inventory_config->db_prefix}shops");//所有仓库
    foreach ($shops = $db->getRows()  as $item){
    	$shop[$item['id']]=$item['title'];
    }
    $time=time();
   // $shopid, $shop, $productid, $product, $spec, $unit, $number, $memo,$type, $thedate
    foreach ($product as $item) {
    	$_t = explode('_', $item);
    	if ($stock_shop){
    		$shopid =$stock_shop;
    	}else{
    		$db->query("select shopid,stocks from {$inventory_config->db_prefix}stocks where productid=$_t[0] and stocks>0 order by shopid asc limit 1");//查询那个仓库有货
    		if($stocks = $db->fetchRow()){
    			$shopid=$stocks['shopid'];
    		}
    		else{//默认是第一个库
    			$_tmp=array_keys($shop);
    			$shopid=$_tmp[0];
    		}
    	}
    	
    	$shopname=$shop[$shopid];
        $db->query("select a.unit,b.pname as title,b.spec from {$inventory_config->db_prefix}products as a left join ".SETMEAL." as b  on a.product_sku_id=b.id where a.product_sku_id=$_t[0]");
        if($product_info=$db->fetchRow()){
        		if(empty($_t[1])){
        			$_t[1]=1;
        		}
        		$product_info[title]=real_escape_string($product_info[title]);
            $sqlstr = "insert into {$inventory_config->db_prefix}orders (shopid, shop, productid,lotnumber, product, spec, unit, number, memo,type, thedate) value ('$shopid', '$shopname', '$_t[0]','$oid', '$product_info[title]', '$product_info[spec]', '$product_info[unit]', $_t[1]*$num, 'orderid:$oid',0, '$time')";
            $db->query($sqlstr);
        }
    }
}
//处理特殊字符
function real_escape_string($string){
	if(get_magic_quotes_gpc()){
		return $string;
	}else{
		return mysql_real_escape_string(stripcslashes($string));
	}
}
?>